package com.lenovo.smarttraffic.ui.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.util.FormatUtils;
import com.lenovo.smarttraffic.util.ToastUtils;

import java.util.Map;

public class FindPwdActivity extends BaseActivity {

    private EditText mEditAccount;
    private EditText mEditEmail;

    private Button mBtnFind;


    @Override
    protected int getLayout() {
        return R.layout.activity_findpwd;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();


        //用户名不能少于4位数
        mEditAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                char[] chars = s.toString().toCharArray();
                if (chars.length < 4) {
                    mEditAccount.setError("当前账号名小于4位");
                }
                String regex = "[0-9a-zA-Z]";
                for (char aChar : chars) {
                    if (!(String.valueOf(aChar)).matches(regex)) {
                        mEditAccount.setError("账号只能包括大小写字母和数字");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        //验证邮箱
        mEditEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!FormatUtils.isEmail(s.toString())) {
                    mEditEmail.setError("请输入正确的邮箱");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        mBtnFind.setOnClickListener(v -> {
            String account = mEditAccount.getText().toString();
            String email = mEditEmail.getText().toString();
            if (!FormatUtils.isAccount(account) && (!FormatUtils.isEmail(email))) {
                ToastUtils.showToast(v.getContext(), "您的输入并不符合规范", Toast.LENGTH_SHORT);
            } else {
                SharedPreferences register = getSharedPreferences(Constant.REGISTER, MODE_PRIVATE);
                String value = register.getString("value", null);
                if (value == null) {
                    ToastUtils.showToast(v.getContext(), "您并未注册", Toast.LENGTH_SHORT);
                } else {
                    Map map = new Gson().fromJson(value, Map.class);
                    if (map.get("account").toString().equals(account) && (map.get("email").toString().equals(email))) {
                        ToastUtils.showToast(v.getContext(), String.format("您的密码为%s", map.get("pwd")), Toast.LENGTH_SHORT);
                    } else {
                        ToastUtils.showToast(v.getContext(), "您的账号或邮箱错误", Toast.LENGTH_SHORT);
                    }
                }

            }
        });

    }

    private void initView() {

        initToolBar(this, R.string.find_pwd);

        mEditAccount = (EditText) findViewById(R.id.edit_account);
        mEditEmail = (EditText) findViewById(R.id.edit_email);

        mBtnFind = (Button) findViewById(R.id.findpwdButton);
    }


}
